package com.zhanbo.zhanbowymusic.search;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.PageSongListBean;
import com.mwx.baselib.bean.PlayListBean;
import com.mwx.baselib.bean.PlayListItemBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSearchSongListBinding;
import com.zhanbo.zhanbowymusic.main.bean.FeaturedPlaylistDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.featured.adapter.SingerAlbumPlayListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;
import com.zhanbo.zhanbowymusic.search.adapter.SearchPlayListAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 搜索歌单列表
 */
public class SearchPlayListFragment extends BaseFragment<FragmentSearchSongListBinding> {


    private SearchPlayListAdapter adapter;

    public static SearchPlayListFragment newInstance() {
        SearchPlayListFragment fragment = new SearchPlayListFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }





    @Override
    protected Object getLayout() {
        return R.layout.fragment_search_song_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initData() {

    }
    public void getData(String value){
        Map<String, String> map = new HashMap<>();
        map.put("keyword", value);
        NetworkManager.getInstance().getApiService().playlist(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<PlayListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<PlayListBean> listBaseResponses) {
                        adapter.getItems().clear();
                        adapter.getItems().addAll(listBaseResponses.getData().getItems());
                        adapter.notifyDataSetChanged();
                        if (adapter.getItems().size()<1){
                            binding.recyclerSingleSong.setVisibility(View.GONE);
                                binding.tvNoData.setVisibility(View.VISIBLE);
                        }else {
                            binding.tvNoData.setVisibility(View.GONE);
                            binding.recyclerSingleSong.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }
    @Override
    protected void initView() {
        binding.recyclerSingleSong.setLayoutManager(new GridLayoutManager(_mActivity, 5));
        adapter = new SearchPlayListAdapter(_mActivity);
        binding.recyclerSingleSong.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        adapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<PlayListItemBean>() {
            @Override
            public void onItemClick(int position) {
                EventBus.getDefault().post(new FeaturedPlaylistDetailsEBBean(adapter.getItems().get(position)));

            }
        });
    }
}